package com.intelligent.ispc.r.analysis.grr;

import com.intelligent.ispc.common.mapper.JsonMapper;
import com.intelligent.ispc.common.utils.StringUtils;
import com.intelligent.ispc.utils.RuleLevelType;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Peter on 2016/6/4.
 */
public class GrrConfiguration {
    private int digitNum = 4;
    private List<Double> rules;
    private JsonMapper mapper = new JsonMapper();

    /**
     * @param dataValue the value
     * @return string
     */
    public String getLevelType(double dataValue) {
        if (rules != null && !rules.isEmpty()) {
            if (dataValue < rules.get(0)) {
                return RuleLevelType.EXCELLENT.toString();
            } else if (rules.get(0) <= dataValue && dataValue < rules.get(1)) {
                return RuleLevelType.GOOD.toString();
            } else if (rules.get(1) <= dataValue && dataValue < rules.get(2)) {
                return RuleLevelType.ACCEPTABLE.toString();
            } else if (rules.get(2) <= dataValue) {
                return RuleLevelType.RECTIFICATION.toString();
            }
        }

        return RuleLevelType.RECTIFICATION.toString();
    }

    /**
     * @param dataValue the value
     * @return string
     */
    public double getDataValue(String dataValue) {
        if (digitNum > 0 && StringUtils.isNumeric(dataValue)) {
            return new BigDecimal(dataValue).setScale(digitNum, BigDecimal.ROUND_HALF_UP).doubleValue();
        } else {
            return Double.valueOf(dataValue);
        }
    }

    public void setRule(String rule) {
        rules = mapper.fromJson(rule, JsonMapper.nonEmptyMapper().contructCollectionType(List.class, Double.class));
    }

    public int getDigitNum() {
        return digitNum;
    }

    public void setDigitNum(int digitNum) {
        this.digitNum = digitNum;
    }
}
